<template>
  <ContentWrap>
    <Search
      :model="{}"
      :schema="allSchemas.searchSchema"
      @search="setSearchParams"
      @reset="setSearchParams"
    />

    <div class="mb-10px">
      <ElButton :loading="delLoading" type="primary" @click="delData(null, true, '18-1')">
        {{ t('customClearanceForm.import') }}
      </ElButton>
      <ElButton type="primary" @click="AddAction('18-1')">{{ t('exampleDemo.add') }}</ElButton>
      <ElButton :loading="delLoading" type="danger" @click="delData(null, true, '18-3')">
        {{ t('exampleDemo.del') }}
      </ElButton>
    </div>

    <Table
      v-model:pageSize="tableObject.limit"
      v-model:currentPage="tableObject.currentPage"
      :columns="allSchemas.tableColumns"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      @register="register"
    >
      <template #emp="{ row }"> {{ type(row.emp) }} </template>
      <template #action="{ row }">
        <ElButton type="primary" @click="action(row, 'edit', '18-2')">
          {{ t('exampleDemo.edit') }}
        </ElButton>
        <ElButton type="danger" @click="delData(row, false, '18-3')">
          {{ t('exampleDemo.del') }}
        </ElButton>
      </template>
    </Table>
  </ContentWrap>
</template>
<script setup lang="ts">
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { ElButton, ElMessage } from 'element-plus'

import { Table } from '@/components/Table'
// import { clientIndex, clientDEL } from '@/api/common'
import { user_brokerageIndex, user_brokerageDel } from '@/api/common'
import { useTable } from '@/hooks/web/useTable'
// import { user_brokerageType } from '@/api/table/types'
import { user_brokerageType } from '@/api/common/types'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { ref, reactive } from 'vue'
import { useI18n } from '@/hooks/web/useI18n'
import { useRouter } from 'vue-router'
import { useEmitt } from '@/hooks/web/useEmitt'

import { useAppStoreWithOut } from '@/store/modules/app'
import { useCache } from '@/hooks/web/useCache'
const appStore = useAppStoreWithOut()
const { wsCache } = useCache()
//用户权限数组
const userRlues = wsCache.get(appStore.getUserInfo).rlues
const userId = wsCache.get(appStore.getUserInfo).id
const checkRlues = (rlues) => {
  //检查权限
  if (userRlues.includes(rlues)) {
    return true
  } else {
    ElMessage.error('当前用户无权限操作，请联系管理员')
    return false
  }
}

const { t } = useI18n()
const action = (row: user_brokerageType, type: string, rules: string) => {
  if (!checkRlues(rules)) return false
  dialogTitle.value = t(type === 'edit' ? 'exampleDemo.edit' : 'exampleDemo.detail')
  actionType.value = type
  tableObject.currentRow = row
  dialogVisible.value = true
}
const { register, tableObject, methods } = useTable<user_brokerageType>({
  getListApi: user_brokerageIndex,
  delListApi: user_brokerageDel,
  response: {
    list: 'data',
    total: 'total'
  },
  defaultParams: {
    title: ''
  },
  find: ''
})

const { push } = useRouter()

const { getList, setSearchParams } = methods
const emp = [
  { label: '员工', value: '0' },
  { label: '客户', value: '1' }
]

getList()

useEmitt({
  name: 'getList',
  callback: (type: string) => {
    if (type === 'add') {
      tableObject.currentPage = 1
    }
    getList()
  }
})

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'index',
    label: t('tableDemo.index'),
    width: '120px',

    type: 'index',
    form: {
      show: false
    },
    detail: {
      show: false
    }
  },
  {
    field: 'type_text',
    width: '140px',
    label: t('rebateUsers.customerType'),
    search: {
      show: true
    },
    form: {
      colProps: {
        span: 24
      },
      component: 'Select',
      componentProps: {
        options: emp
      }
    },
    detail: {
      span: 24
    }
  },
  {
    field: 'rate',
    width: '140px',
    label: t('rebateUsers.rebateRatio'),
    search: {
      show: true
    },
    form: {
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24
    }
  },
  {
    field: 'username',
    width: '140px',
    label: t('rebateUsers.userName'),
    search: {
      show: true
    },
    form: {
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24
    }
  }
])

const { allSchemas } = useCrudSchemas(crudSchemas)
const AddAction = (rules) => {
  if (!checkRlues(rules)) return false
  push('/personnelManagement/rebateUsersAdd')
}
const type = (id) => {
  if (id) {
    let list = [
      { label: '员工', value: '0' },
      { label: '客户', value: '1' }
    ]
    let data: any = list.find((item: any) => {
      return String(item.value) == id
    })
    return data.label
  } else {
    return ''
  }
}

const actionType = ref('')

const dialogTitle = ref('')

const dialogVisible = ref(false)
const delLoading = ref(false)
const getArr = (selections) => {
  let a = ''
  selections.map((v) => (a += v.id + ','))
  return a
}

const delData = async (row: user_brokerageType | null, multiple: boolean, rules: string) => {
  if (!checkRlues(rules)) return false
  tableObject.currentRow = row
  const { delList, getSelections } = methods
  const selections = await getSelections()
  delLoading.value = true
  await delList(
    multiple ? getArr(selections) : (tableObject.currentRow?.id as string),
    multiple
  ).finally(() => {
    delLoading.value = false
  })
}

// const action = (row: user_brokerageType, type: string) => {
//   console.log('---row---', row)
//   push(`/personnelManagement/customerManagement${type}?id=${row.id}`)
// }
</script>
<style></style>
